
require 'test/unit/ui/testrunnermediator'
require 'test/unit/ui/testrunnerutilities'

# fugly hacks, don't look :)


module ::Test
  module Unit
	module UI
	  module Html

		# Runs a Test::Unit::TestSuite on the console.
		class TestRunner
		  extend TestRunnerUtilities

      def self.run(test)
        super(test)
        @@output.join('')
      end

		  # Creates a new TestRunner for running the passed
		  # suite. If quiet_mode is true, the output while
		  # running is limited to progress dots, errors and
		  # failures, and the final result. io specifies
		  # where runner output should go to; defaults to
		  # STDOUT.
		  def initialize(suite, output_level=NORMAL, io=STDOUT)
        if (suite.respond_to?(:suite))
          @suite = suite.suite
        else
          @suite = suite
        end
        @output_level = output_level
        @io = io
        @@output = []
        @faults = []
        @name = nil
        @progress_indication = ''
		  end

		  # Begins the test run.
		  def start
        #output_raw("<pre style='font-family:consolas, courier new'>")
        setup_mediator
        attach_to_mediator
        return start_mediator
		  end

		  private
        def setup_mediator
        @mediator = create_mediator(@suite)
        suite_name = @suite.to_s
        if @suite.kind_of?(Module)
          suite_name = @suite.name
        end
        @suite_name = suite_name
        #output_raw(suite_name+' ')
		  end

		  def create_mediator(suite)
        return TestRunnerMediator.new(suite)
		  end

		  def attach_to_mediator
        @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
        @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
        @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
        @mediator.add_listener(TestCase::STARTED, &method(:test_started))
        @mediator.add_listener(TestCase::FINISHED, &method(:test_finished))
		  end

		  def start_mediator
        return @mediator.run_suite
		  end

		  def add_fault(fault)
        @faults << fault
        @progress_indication << fault.single_character_display
		  end

		  def started(result)
        @result = result
		  end

      def finished(elapsed_time)
        @color = has_faults? ? 'result-red' : 'result-green'
        @html = ['<div class="result '+@color+'">']
        @html << @suite_name
        @html << ' '
        @html << @progress_indication
        @html << ' '
        if has_faults?
          @html << '<pre class="faults">'
          @faults.each_with_index do |fault, index|
            @html << "\n"+CGI.escapeHTML('%3d) %s' % [index + 1, fault.long_display])
          end
          @html << '</pre>'
        end
        #@html << @result.to_s
        @html << '</div>'
        @@output = @html
		  end

      def has_faults?
        @faults.length > 0
      end

		  def test_started(name)
        #output_single(name + ": ", VERBOSE)
      end

		  def test_finished(name)
        @progress_indication << '.'
		  end

		end
	  end
	end
  end
end
